package com.idlecode.panel.tool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.idlecode.service.ToolService;
import com.idlecode.util.UUIDUtil;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBScrollPane;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ItemEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-11-07 08:53
 **/
public class UUIdPanel extends JPanel {
    private JButton UUIDV1JButton;

    private JButton UUIDV3JButton;

    private JButton UUIDV4JButton;

    private JButton UUIDV5JButton;

    private JPanel UUIDVersionExtendPanel;

    private JButton dnsJButton;

    private JButton urlJButton;

    private JButton oidJButton;

    private JButton x500JButton;

    private JTextField nameSpaceValueTField;

    private JTextArea UUIDsTextArea;

    private static final Color color = new Color(151, 195, 243);

    public UUIdPanel() {
        JPanel UUIDVersionPanel = new JPanel();
        UUIDVersionPanel.setLayout(new BoxLayout(UUIDVersionPanel, BoxLayout.X_AXIS));
        UUIDVersionPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        JLabel UUIDVersionLabel = new JLabel("UUID版本: ");
        UUIDVersionPanel.add(UUIDVersionLabel);

        UUIDV1JButton = new JButton("v1");
        UUIDV1JButton.setBackground(null);
        UUIDV1JButton.setPreferredSize(new Dimension(35, 35));
        UUIDV1JButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDVersion", "v1");
            UUIDVersionExtendPanel.setVisible(false);
            UUIDVersionExtendPanel.getParent().revalidate();
            UUIDVersionExtendPanel.getParent().repaint();
            UUIDV1JButton.setBackground(color);
            UUIDV3JButton.setBackground(null);
            UUIDV4JButton.setBackground(null);
            UUIDV5JButton.setBackground(null);
        });
        UUIDV3JButton = new JButton("v3");
        UUIDV3JButton.setBackground(null);
        UUIDV3JButton.setPreferredSize(new Dimension(35, 35));
        UUIDV3JButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDVersion", "v3");
            UUIDVersionExtendPanel.setVisible(true);
            UUIDVersionExtendPanel.getParent().revalidate();
            UUIDVersionExtendPanel.getParent().repaint();
            UUIDV1JButton.setBackground(null);
            UUIDV3JButton.setBackground(color);
            UUIDV4JButton.setBackground(null);
            UUIDV5JButton.setBackground(null);
        });
        UUIDV4JButton = new JButton("v4");
        UUIDV4JButton.setBackground(null);
        UUIDV4JButton.setPreferredSize(new Dimension(35, 35));
        UUIDV4JButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDVersion", "v4");
            UUIDVersionExtendPanel.setVisible(false);
            UUIDVersionExtendPanel.getParent().revalidate();
            UUIDVersionExtendPanel.getParent().repaint();
            UUIDV1JButton.setBackground(null);
            UUIDV3JButton.setBackground(null);
            UUIDV4JButton.setBackground(color);
            UUIDV5JButton.setBackground(null);
        });
        UUIDV5JButton = new JButton("v5");
        UUIDV5JButton.setBackground(null);
        UUIDV5JButton.setPreferredSize(new Dimension(35, 35));
        UUIDV5JButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDVersion", "v5");
            UUIDVersionExtendPanel.setVisible(true);
            UUIDVersionExtendPanel.getParent().revalidate();
            UUIDVersionExtendPanel.getParent().repaint();
            UUIDV1JButton.setBackground(null);
            UUIDV3JButton.setBackground(null);
            UUIDV4JButton.setBackground(null);
            UUIDV5JButton.setBackground(color);
        });
        //获取缓存的信息
        String uuidVersion = ToolService.getInstance().getValue("UUIDVersion");
        if ("v1".equals(uuidVersion)) {
            UUIDV1JButton.setBackground(color);
        } else if ("v3".equals(uuidVersion)) {
            UUIDV3JButton.setBackground(color);
        } else if ("v4".equals(uuidVersion)) {
            UUIDV4JButton.setBackground(color);
        } else if ("v5".equals(uuidVersion)) {
            UUIDV5JButton.setBackground(color);
        } else {
            UUIDV1JButton.setBackground(color);
        }
        UUIDVersionPanel.add(UUIDV1JButton);
        // 增加10像素的间距
        UUIDVersionPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        UUIDVersionPanel.add(UUIDV3JButton);
        // 增加10像素的间距
        UUIDVersionPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        UUIDVersionPanel.add(UUIDV4JButton);
        // 增加10像素的间距
        UUIDVersionPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        UUIDVersionPanel.add(UUIDV5JButton);
        // 增加30像素的间距
        UUIDVersionPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        JBCheckBox includeHyphensCheckBox = new JBCheckBox("包含 \"-\"");
        String uuidIncludeHyphens = ToolService.getInstance().getValue("UUIDIncludeHyphens");
        if (StrUtil.isNotBlank(uuidIncludeHyphens)) {
            includeHyphensCheckBox.setSelected(Boolean.parseBoolean(uuidIncludeHyphens));
        }
        includeHyphensCheckBox.addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                ToolService.getInstance().put("UUIDIncludeHyphens", "true");
            } else {
                ToolService.getInstance().put("UUIDIncludeHyphens", "false");
            }
        });

           /*     .addActionListener(e -> {
            boolean selected = includeHyphensCheckBox.isSelected();

        });*/
        UUIDVersionPanel.add(includeHyphensCheckBox);
        UUIDVersionPanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(UUIDVersionPanel);

        UUIDVersionExtendPanel = new JPanel();
        UUIDVersionExtendPanel.setLayout(new BoxLayout(UUIDVersionExtendPanel, BoxLayout.Y_AXIS));
        UUIDVersionExtendPanel.setPreferredSize(new Dimension(0, 93));

        JPanel nameSpacePanel = new JPanel();
        nameSpacePanel.setLayout(new BoxLayout(nameSpacePanel, BoxLayout.X_AXIS));
        nameSpacePanel.add(Box.createRigidArea(new Dimension(12, 0)));
        JLabel nameSpaceLabel = new JLabel("命名空间: ");
        nameSpacePanel.add(nameSpaceLabel);
        dnsJButton = new JButton("DNS");
        dnsJButton.setBackground(null);
        dnsJButton.setPreferredSize(new Dimension(45, 35));
        dnsJButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDNameSpace", "DNS");
            nameSpaceValueTField.setText("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
            dnsJButton.setBackground(color);
            urlJButton.setBackground(null);
            oidJButton.setBackground(null);
            x500JButton.setBackground(null);
        });
        urlJButton = new JButton("URL");
        urlJButton.setBackground(null);
        urlJButton.setPreferredSize(new Dimension(45, 35));
        urlJButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDNameSpace", "URL");
            nameSpaceValueTField.setText("6ba7b811-9dad-11d1-80b4-00c04fd430c8");
            dnsJButton.setBackground(null);
            urlJButton.setBackground(color);
            oidJButton.setBackground(null);
            x500JButton.setBackground(null);
        });
        oidJButton = new JButton("OID");
        oidJButton.setBackground(null);
        oidJButton.setPreferredSize(new Dimension(45, 35));
        oidJButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDNameSpace", "OID");
            nameSpaceValueTField.setText("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
            dnsJButton.setBackground(null);
            urlJButton.setBackground(null);
            oidJButton.setBackground(color);
            x500JButton.setBackground(null);
        });
        x500JButton = new JButton("X500");
        x500JButton.setBackground(null);
        x500JButton.setPreferredSize(new Dimension(45, 35));
        x500JButton.addActionListener(e -> {
            ToolService.getInstance().put("UUIDNameSpace", "X500");
            nameSpaceValueTField.setText("6ba7b814-9dad-11d1-80b4-00c04fd430c8");
            dnsJButton.setBackground(null);
            urlJButton.setBackground(null);
            oidJButton.setBackground(null);
            x500JButton.setBackground(color);
        });

        //获取缓存的信息
        String UUIDNameSpace = ToolService.getInstance().getValue("UUIDNameSpace");
        if ("DNS".equals(UUIDNameSpace)) {
            SwingUtilities.invokeLater(() -> {
                dnsJButton.setBackground(color);
                nameSpaceValueTField.setText("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
            });
        } else if ("URL".equals(UUIDNameSpace)) {
            SwingUtilities.invokeLater(() -> {
                urlJButton.setBackground(color);
                nameSpaceValueTField.setText("6ba7b811-9dad-11d1-80b4-00c04fd430c8");
            });
        } else if ("OID".equals(UUIDNameSpace)) {
            SwingUtilities.invokeLater(() -> {
                oidJButton.setBackground(color);
                nameSpaceValueTField.setText("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
            });
        } else if ("X500".equals(UUIDNameSpace)) {
            SwingUtilities.invokeLater(() -> {
                x500JButton.setBackground(color);
                nameSpaceValueTField.setText("6ba7b814-9dad-11d1-80b4-00c04fd430c8");
            });
        } else {
            SwingUtilities.invokeLater(() -> {
                dnsJButton.setBackground(color);
                nameSpaceValueTField.setText("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
            });
        }

        nameSpacePanel.add(dnsJButton);
        // 增加10像素的间距
        nameSpacePanel.add(Box.createRigidArea(new Dimension(5, 0)));
        nameSpacePanel.add(urlJButton);
        // 增加10像素的间距
        nameSpacePanel.add(Box.createRigidArea(new Dimension(5, 0)));
        nameSpacePanel.add(oidJButton);
        // 增加10像素的间距
        nameSpacePanel.add(Box.createRigidArea(new Dimension(5, 0)));
        nameSpacePanel.add(x500JButton);
        nameSpacePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        UUIDVersionExtendPanel.add(nameSpacePanel);

        JPanel nameSpaceValuePanel = new JPanel();
        nameSpaceValuePanel.setLayout(new BoxLayout(nameSpaceValuePanel, BoxLayout.X_AXIS));
        nameSpaceValuePanel.add(Box.createRigidArea(new Dimension(67, 0)));
        nameSpaceValueTField = new JTextField();
        nameSpaceValueTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        nameSpaceValuePanel.add(nameSpaceValueTField);
        UUIDVersionExtendPanel.add(nameSpaceValuePanel);

        JPanel namePanel = new JPanel();
        namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS));
        namePanel.add(Box.createRigidArea(new Dimension(36, 0)));
        JLabel nameLabel = new JLabel("名称: ");
        namePanel.add(nameLabel);
        JTextField nameTField = new JTextField();
        nameTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        namePanel.add(nameTField);
        UUIDVersionExtendPanel.add(namePanel);
        UUIDVersionExtendPanel.setVisible(false);
        this.add(UUIDVersionExtendPanel);
        if ("v1".equals(uuidVersion) || "v4".equals(uuidVersion)) {
            SwingUtilities.invokeLater(() -> {
                UUIDVersionExtendPanel.setVisible(false);
                UUIDVersionExtendPanel.getParent().revalidate();
                UUIDVersionExtendPanel.getParent().repaint();
            });
        } else if ("v3".equals(uuidVersion) || "v5".equals(uuidVersion)) {
            SwingUtilities.invokeLater(() -> {
                UUIDVersionExtendPanel.setVisible(true);
                UUIDVersionExtendPanel.getParent().revalidate();
                UUIDVersionExtendPanel.getParent().repaint();
            });
        }

        JPanel numPanel = new JPanel();
        numPanel.setLayout(new BoxLayout(numPanel, BoxLayout.X_AXIS));
        numPanel.add(Box.createRigidArea(new Dimension(36, 0)));
        JLabel UUIDNumLabel = new JLabel("数量: ");
        numPanel.add(UUIDNumLabel);
        JTextField UUIDNumTField = new JTextField();
        // 添加KeyListener来限制输入
        UUIDNumTField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();

                // 仅允许输入数字字符
                if (!Character.isDigit(c)) {
                    e.consume();
                    return;
                }

                // 如果输入后结果为0，阻止输入
                String text = UUIDNumTField.getText() + c;
                try {
                    int value = Integer.parseInt(text);
                    if (value <= 0) {
                        e.consume();
                    }
                } catch (NumberFormatException ex) {
                    e.consume(); // 防止异常值
                }
            }
        });

        UUIDNumTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
        String UUIDNum = ToolService.getInstance().getValue("UUIDNum");

        if (StrUtil.isNotBlank(UUIDNum)) {
            UUIDNumTField.setText(UUIDNum);
        } else {
            UUIDNumTField.setText("1");
        }

        numPanel.add(UUIDNumTField);
        numPanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(numPanel);

        JPanel operateBtnsPanel = new JPanel();
        operateBtnsPanel.setLayout(new BoxLayout(operateBtnsPanel, BoxLayout.X_AXIS));
        operateBtnsPanel.add(Box.createHorizontalGlue()); // 左侧弹性空间
        JButton copyJButton = new JButton("复制");
        copyJButton.addActionListener(e -> {
            String text = UUIDsTextArea.getText();
            if (StrUtil.isNotBlank(text)) {
                // 将文本复制到剪贴板
                StringSelection stringSelection = new StringSelection(text);
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
            }
        });
        operateBtnsPanel.add(copyJButton);
        JButton generateJButton = new JButton("生成");
        generateJButton.addActionListener(e -> {
            String UUIDNumNow = UUIDNumTField.getText();
            if (StrUtil.isBlank(UUIDNumNow)) {
                Messages.showMessageDialog("数量不能为空！", "警告", Messages.getWarningIcon());
                return;
            }
            ToolService.getInstance().put("UUIDNum", UUIDNumNow);
            int version = 0;
            if (UUIDV1JButton.isBackgroundSet()) {
                version = 1;
            } else if (UUIDV3JButton.isBackgroundSet()) {
                version = 3;
            } else if (UUIDV4JButton.isBackgroundSet()) {
                version = 4;
            } else if (UUIDV5JButton.isBackgroundSet()) {
                version = 5;
            }
            List<String> UUIDS = UUIDUtil.generateUUIDs(version, Integer.valueOf(UUIDNumNow).intValue(), nameSpaceValueTField.getText(),
                    nameTField.getText(), includeHyphensCheckBox.isSelected());
            UUIDsTextArea.setText(null);
            // 获取当前字体并设置新的大小
            Font currentFont = UUIDsTextArea.getFont();
            UUIDsTextArea.setFont(currentFont.deriveFont(15f)); // 仅设置字体大小为 20
            if (CollUtil.isNotEmpty(UUIDS)) {
                for (String uuid : UUIDS) {
                    UUIDsTextArea.append(uuid);
                    UUIDsTextArea.append("\n");
                }
            }

        });
        operateBtnsPanel.add(generateJButton);
        operateBtnsPanel.add(Box.createHorizontalGlue()); // 右侧弹性空间

        this.add(operateBtnsPanel);

        UUIDsTextArea = new JTextArea();
        //UUIDsTextArea.setFont(new Font(Font.DE));

        JScrollPane scrollPane = new JBScrollPane(UUIDsTextArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        this.add(scrollPane);
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    }
}
